package com.learn.redis.controller;

import com.learn.redis.vo.Student;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class RedisController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/redis/addstring")
    public String addToRedis(String name, String value){
        // 操作Redis中String类型的数据，先获取 ValueOperations 对象
        ValueOperations valueOperations = redisTemplate.opsForValue();

        // 添加数据到redis
        valueOperations.set(name, value);
        return  "向redis中添加数据";
    }
    // 从redis中读取数据
    @GetMapping("/redis/getk")
    public String getData(String k){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object myVal = (String) valueOperations.get(k);
        return "key:"+ k + ", 的值value是：" + myVal;
    }

    /**
     * 使用的 StringRedisTemplate
     */
    @PostMapping("/redis/{k}/{v}")
    public String addStringKV(@PathVariable("k") String k,
                              @PathVariable("v") String v){
        stringRedisTemplate.opsForValue().set(k, v);
        return  "使用stringRedisTemplate创建数据";
    }

    @GetMapping("/redis/{k}")
    public String getStingK(@PathVariable("k") String k){
        String v = stringRedisTemplate.opsForValue().get(k);
        return "k的value：" + v;
    }

    /**
     * 设置RedisTemplate 序列化
     * 可以设置key和value的序列化
     */
    @PostMapping("/redis/addstr")
    public String addString(String k, String v){
        // 使用RedisTemplate, 设置 key 的String序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //使用RedisTemplate, 设置 value 的String序列化
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set(k,v);
        return  "定义redisTemplate 对象的key、value的序列化";
    }

    @GetMapping("/redis/addstr")
    public String getRedisTempString(String k){
        // 使用RedisTemplate, 设置 key 的String序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //使用RedisTemplate, 设置 value 的String序列化
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        Object o = redisTemplate.opsForValue().get(k);
        return "key值为："+ k +"的值value为：" + o;
    }

    /**
     * 使用json序列化，把java对象转为json
     */
    @PostMapping("/redis/setJson")
    public String setJson(){
        Student student = new Student();
        student.setId(1001);
        student.setName("json");
        student.setAge(12);
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        // 把值进行 json 序列化
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));

        redisTemplate.opsForValue().set("myStudent", student);
        return "json 序列化";
    }

    @GetMapping("/redis/getJson")
    public String getJson(){
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        // 把值进行 json 序列化
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));

        Object myStudent = redisTemplate.opsForValue().get("myStudent");
        return "json 序列化取值： " + myStudent.toString();
    }


}
